package com.hhss.qishi.mapper.system.provider;

import com.hhss.qishi.entity.system.Menu;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.StringUtils;

/**
 * @Author :Administrator
 * @Description
 * @Date :2019-05-07 16:40.
 */
public class MenuProvider {

	private static String SQL_ENTITY = "id, uuid,"
			+ " pid, menu_name as menuName, menu_level AS menuLevel, menu_path AS menuPath, menu_url AS menuUrl,"
			+ " menu_type AS menuType, menu_sort AS menuSort, menu_info AS menuInfo,"
			+ " status, create_user AS createUser, create_time AS createTime, last_update_user AS lastUpdateUser,"
			+ " last_update_time AS lastUpdateTime, create_user AS createUser, last_update_user AS lastUpdateUser";


	private static String TABLE_NAME = "sys_menu";

	/**
	 * 查找角色集合
	 * @param menu
	 * @return
	 */
	public String getListSQL(final Menu menu) {
		return new SQL(){{
			SELECT(SQL_ENTITY);
			FROM(TABLE_NAME);
			WHERE("1=1");
			if(!StringUtils.isEmpty(menu.getMenuName())) {
				AND().WHERE("menu_name like CONCAT('%',#{menu.menuName},'%')");
			}
			if(!StringUtils.isEmpty(menu.getPid())) {
				AND().WHERE("pid = #{menu.pid}");
			}
		}}.toString();
	}

	/**
	 * 根据pid查找下属菜单
	 * @param menu
	 * @return
	 */
	public String getMenuTreeByPidSQL(final Menu menu) {
		return new SQL(){{
			SELECT("id, uuid, pid, menu_name as menuName, menu_level AS menuLevel, menu_path AS menuPath," +
					" menu_url AS menuUrl, menu_type AS menuType, menu_sort AS menuSort, menu_info AS menuInfo");
			FROM(TABLE_NAME);
			WHERE("1=1");
			if(!StringUtils.isEmpty(menu.getMenuName())) {
				AND().WHERE("menu_name like CONCAT('%',#{menu.menuName},'%')");
			}
			if(!StringUtils.isEmpty(menu.getPid())) {
				AND().WHERE("pid = #{menu.pid}");
			}
		}}.toString();
	}
}
